import java.util.Random;

import java.util.*;
import java.io.*;
public class test {
    public static void main(String[] args) throws IOException {
        Scanner input=new Scanner(System.in);
        System.out.println("你想合并几个文件?");
        int n=input.nextInt();
        File[] SourceFile=new File[n];
        for(int i=0;i<n;i++) {
            SourceFile[i]=new File("SourceFile"+(i+1)+".txt");
            PrintWriter p=new PrintWriter(SourceFile[i]);
            Random r=new Random();
            for(int j=0;j<10;j++) {
                p.print(r.nextInt(100)+"   ");
            }
            p.close();
        }
        BufferedWriter w=new BufferedWriter(new FileWriter("TargetFile.txt"));
        for(int i=0;i<n;i++) {
            BufferedReader r=new BufferedReader(new FileReader(SourceFile[i]));
            w.write(r.readLine()+"\n");
            r.close();
        }
        w.close();   
    }

    public static void main7(String[] args) throws IOException, ClassNotFoundException{
        int[] a= {1,2,3,4,5};
        double b=5.5;
        @SuppressWarnings("resource")
        ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream("Exercise19_5.dat"));
        output.writeDouble(b);//要先写非对象类型的，否则会报错
        output.writeObject(a);
        output.writeObject(new Date());
        @SuppressWarnings("resource")
        ObjectInputStream input=new ObjectInputStream(new FileInputStream("Exercise19_5.dat"));
        double e=input.readDouble();
        int[] c=(int[])(input.readObject());
        Date d=(Date)(input.readObject());
        System.out.println("数组为：");
        for(int i=0;i<c.length;i++) {
            System.out.print(c[i]+"   ");
        }
        System.out.println();
        System.out.println("当前时间："+d);
        System.out.println("double值："+e);
        output.close();
        input.close();

    }

    public static void main6(String[] args) throws Exception {
        String s;
        BufferedReader r=new BufferedReader(new FileReader("src\\test.java"));
        BufferedWriter w=new BufferedWriter(new FileWriter("temp.text"));
        for (int i = 1;(s=r.readLine())!= null ; i++) {
            w.write(i+":"+s+"\n");
        }
        r.close();
        w.close();
    }

    public static void main5(String[] args) throws Exception {
        double t1=System.currentTimeMillis();
        int n=0;
        File file = new File("bigbook.txt");
        Scanner input=new Scanner(file);
        while(input.hasNext()) {
            String s;
            s=input.nextLine();
            System.out.println(s);
            n+=sort(s);
        }
        input.close();
        double t2=System.currentTimeMillis();
        System.out.println("有"+n+"个A");
        System.out.println("用时为："+(t2-t1)+"ms");
    }

    public static int sort(String s) {
        char a='A';
        int n=0;
        for(int i=0;i<s.length();i++)
            if(a==s.charAt(i))
                n++;
        return n;
    }

    public static void main4(String[] args) throws IOException{
        DataOutputStream output=new DataOutputStream(new FileOutputStream("Exercise 19_3.dat"));
        DataInputStream input=new DataInputStream(new FileInputStream("Exercise 19_3.dat"));
        Random r=new Random();
        int s=0;
        int n=r.nextInt(50)+10;
        System.out.println("文件包含"+n+"个整数");
        for(int i=0;i<n;i++) {
            int m=r.nextInt(100);
            output.writeInt(m);
            System.out.print(input.readInt()+" ");//读出文件的内容并输出
            s+=m;
        }
        output.close();
        input.close();
        System.out.println();
        System.out.println("它们的和为："+s);
    }

    public static void main3(String[] args) throws IOException{
        File f=new File("Exercise 19_2.dat");
        if(!f.exists()) {
            f=new File("Exercise 19_2.txt");
        }
        PrintWriter p=new PrintWriter(f);
        Random r=new Random();
        for(int i=0;i<100;i++) {
            p.print(r.nextInt(10000)+"\n");
        }
        p.close();
    }

    public static void main2(String[] args) throws IOException {
        BufferedOutputStream put = new BufferedOutputStream(
                new FileOutputStream("temp2.dat"));
        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            put.write((int) (Math.random()*10));
        }
        long stop2 = System.currentTimeMillis();
        long time2 = stop2-start2;
        System.out.println("BufferedWriter的时间差为："+ time2 +" 毫秒");
    }
    public static void main1(String[] args) throws IOException {
        FileOutputStream output = new FileOutputStream("temp.dat");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            output.write((int) (Math.random()*10));
        }
        long stop = System.currentTimeMillis();
        long time = stop-start;
        System.out.println("FileWriter的时间差为："+ time +"毫秒");
    }
}
